package org.person.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.person.springcloud.models.CommonResult;
import org.person.springcloud.models.Payment;
import org.person.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class PaymentContoller {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private int port;

    @PostMapping("/payment")
    public CommonResult save(@RequestBody Payment payment) {
        log.info("服务端口：" + port);
        int result = paymentService.save(payment);
        log.info("插入结果" , result);
        if (result > 0 )
            return new CommonResult(200, "插入数据库成功", result);
        return new CommonResult(400, "插入数据库失败", null);
    }

    @GetMapping("/payment/{id}")
    public CommonResult get(@PathVariable("id") Long id) {
        log.info("服务端口：" + port);
        Payment result = paymentService.getById(id);
        log.info("插入结果" , result);
        if (result != null )
            return new CommonResult(200, "数据库查询成功" + port, result);
        return new CommonResult(400, "数据库查询失败", null);
    }

    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "error";
        }
        return "timeout";
    }
}
